package com.gesee.zyspps.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gesee.zys.R;
import com.gesee.zyspps.ZSApplication;
import com.gesee.zyspps.adapter.PointCountAdapter;
import com.gesee.zyspps.db.CmsPoints;
import com.gesee.zyspps.db.CmsPointsName;
import com.gesee.zyspps.db.CmsPointsNameDao.Properties;
import com.gesee.zyspps.model.PointCount;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class PointNumCountActivity extends BaseActivity
{
    private ListView listView;
    private 
    Button btnExit;
    private PointCountAdapter adapter;
    private List<PointCount> pointCountList;
    private Map<String,Integer> map = new HashMap<String,Integer>();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.count_point_num);
        findView();
        initView();
    }

    private void findView()
    {
        pointCountList = new ArrayList<PointCount>();
        btnExit = (Button) findViewById(R.id.btnExit);
        listView = (ListView) findViewById(R.id.listView);
        loadPoint();
        adapter = new PointCountAdapter(pointCountList,this);
        listView.setAdapter(adapter);
    }

    private void initView()
    {
        btnExit.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                ZSApplication.getInstance().finishActivity();
            }
        });
//        loadPoint();
    }

    private void loadPoint()
    {
        
        List<CmsPoints> pointList = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsPointsDao().loadAll();
        for (int i = 0; i < pointList.size(); i++)
        {
            String PCode = pointList.get(i).getPCode();
            
            CmsPointsName cms = ZSApplication.getDaoSession("zyspps-db").getCmsPointsNameDao().queryBuilder().where(Properties.SubCode.eq(PCode)).list().get(0);
            String subName = cms.getSubName();
            if(map.containsKey(PCode + "(" + subName + ")")){
                map.put(PCode + "(" + subName + ")", map.get(PCode + "(" + subName + ")")+1);
            }else{
                map.put(PCode + "(" + subName + ")",1);
            }
            /*CmsPointsName pointName = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsPointsNameDao().queryBuilder().where(Properties.SubCode.eq(PCode)).build()
                    .list().get(0);
            String subName = pointName.getSubName();

            if(map.containsKey("PCode" + "(" + subName + ")")){
                map.put("PCode" + "(" + subName + ")", map.get("PCode" + "(" + subName + ")")+1);
            }else{
                map.put("PCode" + "(" + subName + ")",1);
            }*/
            /*PointCount pointCount = new PointCount();
            pointCount.setType(PCode + "(" + subName + ")");*/
            PointCount pointCount = new PointCount();
        }
        
        java.util.Iterator it = map.entrySet().iterator();
        while(it.hasNext()){
            java.util.Map.Entry entry = (java.util.Map.Entry)it.next();
            PointCount pointCount = new PointCount();
            pointCount.setType(entry.getKey().toString());
            pointCount.setCount(Integer.parseInt(entry.getValue().toString()));
            pointCountList.add(pointCount);
        }
        PointCount pointCount = new PointCount();
        pointCount.setType("总计");
        pointCount.setCount(pointList.size());
        pointCountList.add(pointCount);
        
        
        
    }
}
